﻿using System;
using System.Xml;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for UpdateInfo
/// </summary>
public class UpdateInfo
{
    private DataTable dtList;

    public UpdateInfo(DataTable dt)
	{
        dtList = dt;
	}

    public XmlDocument GetUpdateInfoElement(ArtefactPrimaryKey key)
    {
        XmlDocument xDoc = new XmlDocument();
        StringBuilder sb = new StringBuilder();

        string dt = DateTime.Now.ToString("s");
        // Create Header
        sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        sb.Append("<Structure xmlns=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message\" xmlns:common=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common\" xmlns:structure=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure\" xmlns:utility=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/utility\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd\">");
        sb.Append("<Header><ID>").Append(Guid.NewGuid().ToString()).Append("</ID>");
        sb.Append("<Test>false</Test>");
        sb.Append(" <Prepared>").Append(dt).Append("</Prepared>");
        sb.Append("<Sender id=\"CH1\"></Sender></Header>");

        // Add infos as codes of o codelist
        sb.Append("<CodeLists><structure:CodeList id=\"CL_LAST_UPDATE\" version=\"1.0\" agencyID=\"NSI\">");
        sb.Append("<structure:Name>Last upfate for: ").Append(key.FullIdent).Append("</structure:Name>");

        // For current version and upper versions
        //    Creates code with version Nr and date of last modif and finalized status
        double reqVersion = 0.0;
        if (double.TryParse(key.Version, out reqVersion))
        {
            double version = 0.0;
            foreach (DataRow row in dtList.Rows)
            {
                if (row["AI"].ToString() == key.AgencyId &&
                    row["ID"].ToString() == key.Id)
                {
                    //Version as double, if same or higher then key version add info as code
                    if (double.TryParse(row["VER"].ToString(), out version))
                    {
                        if (version >= reqVersion)
                        {
                            sb.Append("<structure:Code value=\"").Append(version.ToString().Replace('.', '_')).Append("\">");
                            sb.Append("<structure:Description>").Append(row["LU"].ToString());
                            if (row["IF"].ToString() == "True")
                                sb.Append(" =Finalized");
                            sb.Append("</structure:Description></structure:Code>");
                        }
                    }

                }
            }
        }

        // Close tags
        sb.Append("</structure:CodeList></CodeLists></Structure>");

        xDoc.LoadXml(sb.ToString());
        return xDoc;
    }
}
